/*
  scoreboard.cpp - Library for SFDB scoreboard.
  Created by Guido Zuliani (zulio), January 14, 2012.
  Released into the public domain.
*/
#include "scoreboard.h"

ScoreBoard::ScoreBoard()
{
  _init();
}

ScoreBoard::ScoreBoard(unsigned long (*func_external_millis)(void))
{
  _init();
  _func_external_millis = func_external_millis;
}

void ScoreBoard::_init()
{
  _func_external_millis=NULL;
  _func_time_ticker=NULL;
  _func_time_ended=NULL;
  _need_refresh = true;
  
  _home_score = 0;
  _guest_score = 0;
  _arrow_left = true;
  _period = 1;
  _bonus = 0;  
   _end_time_millis = 0;
  _remain_time_millis = (1*60)*1000UL;  //default: 1 minutes per period
  _update_clock(); 
  
  _last_millis = 0;
  
}


void ScoreBoard::_update_clock()
{
  _decimals = DO_NOT_SHOW;
  
  int previous_minutes = _minutes;
  int previous_seconds = _seconds;
  int previous_decimals = _decimals;
 
  _seconds = _remain_time_millis/1000UL;
  _minutes = _seconds / 60;
  _seconds = _seconds % 60;
  //_minutes = _minutes % 60UL;  
  
  if(previous_decimals!=_decimals ||
     previous_seconds!=_seconds    ||
     previous_minutes!=_minutes)
  {
    if(_time_running && _func_time_ticker!=NULL)
      _func_time_ticker();
    _need_refresh = true;
  }
  
  if(_time_running && _remain_time_millis==0)
  {
    if(_func_time_ended!=NULL)
      _func_time_ended();
    stop_time();
    _need_refresh = true;
  }
}

void ScoreBoard::add_home_score(int val)
{
  _home_score+=val;
  _need_refresh = true;
}

void ScoreBoard::add_guest_score(int val)
{
  _guest_score+=val;
  _need_refresh = true;
}

void ScoreBoard::sub_home_score(int val)
{
  if((_home_score>val))
    _home_score-=val;
  else
    _home_score=0;
  _need_refresh = true;
}

void ScoreBoard::sub_guest_score(int val)
{
  if((_guest_score>val))
    _guest_score-=val;
  else
    _guest_score=0;
    
  _need_refresh = true;
}

void ScoreBoard::change_bonus()
{
  if(_bonus==3)
    _bonus=0;
  else
    _bonus++; 
  _need_refresh = true; 
}

void ScoreBoard::change_arrow()
{
  _arrow_left = !_arrow_left;
  _need_refresh = true;
}

void ScoreBoard::change_period()
{
  if(_period==9)
    _period = 1;
  else
    _period++;
  _need_refresh = true;
}

void ScoreBoard::add_time(int seconds)
{
  _remain_time_millis += seconds*1000;
  _update_clock();
}

void ScoreBoard::sub_time(int seconds)
{
  if(_remain_time_millis > seconds*1000)
    _remain_time_millis -= seconds*1000;
  else
    _remain_time_millis = 0;
  _update_clock();
}

void ScoreBoard::stop_or_start_time()
{
  if(_time_running)
    stop_time();
  else
    start_time();
}

void ScoreBoard::stop_time()
{
  //refresh();
  if(_time_running)
     _need_refresh = true;
  _time_running = false;
}

void ScoreBoard::start_time()
{
  //refresh();
  if(!_time_running)
    _need_refresh = true;
  
  _end_time_millis = current_millis() + _remain_time_millis;  
  _time_running = true;
}

unsigned long ScoreBoard::refresh(unsigned long milliseconds)
{
  unsigned long current = milliseconds;

  if(_time_running)
  {
    if(current==0)
       current = current_millis();

    if(current<_end_time_millis)
      _remain_time_millis = _end_time_millis - current;
    else
      _remain_time_millis = 0;
    _update_clock();
  }
    
  return current;
}

void ScoreBoard::set_funct_external_mills(unsigned long (*func_external_millis)(void))
{
  _func_external_millis = func_external_millis;
}

unsigned long ScoreBoard::current_millis()
{
  if(_func_external_millis!=NULL)
    _last_millis = _func_external_millis();
  else
    _last_millis = 0;
 return _last_millis;   
}

